home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / PIL / GdImageFile.py < prev    next >
Encoding:
Text File  |  2000-06-23  |  1.6 KB  |  77 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id: GdImageFile.py,v 1.1.1.1 1998/08/18 13:07:54 sjoerd Exp $
  4. #
  5. # GD file handling
  6. #
  7. # History:
  8. #    96-04-12 fl    Created
  9. #
  10. # Copyright (c) Secret Labs AB 1997.
  11. # Copyright (c) Fredrik Lundh 1996.
  12. #
  13. # See the README file for information on usage and redistribution.
  14. #
  15.  
  16.  
  17. # NOTE: This format cannot be automatically recognized, so the
  18. # class is not registered for use with Image.open().  To open a
  19. # gd file, use the GdImageFile.open() function instead.
  20.  
  21. # THE GD FORMAT IS NOT DESIGNED FOR DATA INTERCHANGE.  This
  22. # implementation is provided for convenience and demonstrational
  23. # purposes only.
  24.  
  25.  
  26. __version__ = "0.1"
  27.  
  28. import string
  29. import Image, ImageFile, ImagePalette
  30.  
  31. def i16(c):
  32.     return ord(c[1]) + (ord(c[0])<<8)
  33.  
  34.  
  35. class GdImageFile(ImageFile.ImageFile):
  36.  
  37.     format = "GD"
  38.     format_description = "GD uncompressed images"
  39.  
  40.     def _open(self):
  41.  
  42.     # Header
  43.     s = self.fp.read(775)
  44.  
  45.     self.mode = "L" # FIXME: "P"
  46.     self.size = i16(s[0:2]), i16(s[2:4])
  47.  
  48.     # transparency index
  49.     tindex = i16(s[5:7])
  50.     if tindex < 256:
  51.         self.info["transparent"] = tindex
  52.  
  53.     self.palette = ImagePalette.raw("RGB", s[7:])
  54.  
  55.     self.tile = [("raw", (0,0)+self.size, 775, ("L", 0, -1))]
  56.  
  57.  
  58. def open(fp, mode = "r"):
  59.     "Open a GD image file, without loading the raster data"
  60.  
  61.     if mode != "r":
  62.         raise ValueError, "bad mode"
  63.  
  64.     if type(fp) == type(""):
  65.         import __builtin__
  66.     filename = fp
  67.         fp = __builtin__.open(fp, "rb")
  68.     else:
  69.         filename = ""
  70.  
  71.     try:
  72.     return GdImageFile(fp, filename)
  73.     except SyntaxError:
  74.     raise IOError, "cannot identify this image file"
  75.  
  76. # save is not supported
  77.